package guis.member;

import exceptions.RepositoryException;
import guis.ComponentFactory;
import guis.ModalWindow;
import guis.RegisterPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import classes.City;
import classes.Classroom;
import classes.District;
import classes.Member;

import communication.Facade;

public class MemberWindow extends ModalWindow
{
	private static final long	serialVersionUID	= 1032307379765488755L;
	protected JTextField		nameTextField;
	protected JTextField		phoneTextField;
	protected JTextField		eMailTextField;
	protected JTextField		streetTextField;
	protected JTextField		numberTextField;
	protected JTextField		cepTextField;

	protected ButtonGroup		genderGroup;
	protected JRadioButton		maleRadioButton;
	protected JRadioButton		femaleRadioButton;

	protected JComboBox			dayComboBox;
	protected JComboBox			monthComboBox;
	protected JComboBox			yearComboBox;
	protected JComboBox			classroomComboBox;
	protected JComboBox			districtComboBox;
	protected JComboBox			cityComboBox;
	
	protected JButton			actionButton;
	protected JButton			cancelButton;
	
	protected JCheckBox			comungantCheckBox;
	
	protected RegisterPanel< Member >		memberPanel;
	protected Facade			facade;

	public MemberWindow( String title, String actionButtonFunction, JFrame source, RegisterPanel< Member > registerPanel, Facade facade )
	{
		super( title, source );
		this.facade = facade;
		this.memberPanel = registerPanel;
		initialize();
		createLabels();
		createTextFields();
		createComboBoxes();
		createButtons( actionButtonFunction );
		createRadioButtons();
		createCheckBox();
	}

	private void initialize()
	{
		this.setSize( 490, 340 );
		this.setLocationRelativeTo( null );
	}

	private void createLabels()
	{
		ComponentFactory.createLabel( "Nome:", 10, 10, getContentPane() );
		ComponentFactory.createLabel( "Nascimento:", 10, 70, getContentPane() );
		ComponentFactory.createLabel( "/", 175, 70, getContentPane() );
		ComponentFactory.createLabel( "/", 275, 70, getContentPane() );
		ComponentFactory.createLabel( "Sexo:", 10, 40, getContentPane() );
		ComponentFactory.createLabel( "Telefone:", 10, 100, getContentPane() );
		ComponentFactory.createLabel( "E-Mail:", 200, 100, getContentPane() );
		ComponentFactory.createLabel( "Cidade:", 10, 130, getContentPane() );
		ComponentFactory.createLabel( "Bairro:", 10, 160, getContentPane() );
		ComponentFactory.createLabel( "Rua:", 10, 190, getContentPane() );
		ComponentFactory.createLabel( "Número:", 10, 220, getContentPane() );
		ComponentFactory.createLabel( "CEP:", 200, 220, getContentPane() );
		ComponentFactory.createLabel( "Comungante:", 10, 250, getContentPane() );
		ComponentFactory.createLabel( "Sala:", 10, 280, getContentPane() );
	}

	private void createTextFields()
	{
		this.nameTextField = ComponentFactory.createJTextField( 250, 22, 100, 10, getContentPane() );
		this.phoneTextField = ComponentFactory.createJTextField( 85, 22, 100, 100, getContentPane() );
		this.eMailTextField = ComponentFactory.createJTextField( 85, 22, 265, 100, getContentPane() );
		this.streetTextField = ComponentFactory.createJTextField( 250, 22, 100, 190, getContentPane() );
		this.numberTextField = ComponentFactory.createJTextField( 85, 22, 100, 220, getContentPane() );
		this.cepTextField = ComponentFactory.createJTextField( 85, 22, 265, 220, getContentPane() );
	}

	private void createComboBoxes()
	{
		Vector< Classroom > classrooms = new Vector< Classroom >();
		Vector< City > cities = new Vector< City >();
		try
		{
			classrooms = facade.getIteratorClassroom().toVector();
			classrooms.add( 0, null );
			
			cities = facade.getIteratorCity().toVector();
			cities.add( 0, null );
		}
		catch ( RepositoryException e ) {}
		
		Integer[] days = new Integer[ 32 ];
		Integer[] months = new Integer[ 13 ];
		Integer[] years = new Integer[ 150 ];

		for ( int i = 1; i < days.length; i++ ) days[ i ] = i;
		for ( int i = 1; i < months.length; i++ ) months[ i ] = i;
		for ( int i = 1; i < years.length; i++ ) years[ i ] = i + 1920;

		this.dayComboBox = ComponentFactory.createJComboBox( days, 50, 22, 100, 70, getContentPane() );
		this.monthComboBox = ComponentFactory.createJComboBox( months, 50, 22, 200, 70, getContentPane() );
		this.yearComboBox = ComponentFactory.createJComboBox( years, 50, 22, 300, 70, getContentPane() );
		this.classroomComboBox = ComponentFactory.createJComboBox( classrooms, 250, 22, 100, 280, getContentPane() );
		
		this.districtComboBox = ComponentFactory.createJComboBox( new String[]{}, 250, 22, 100, 160, getContentPane() );
		this.cityComboBox = ComponentFactory.createJComboBox( cities, 250, 22, 100, 130, getContentPane() );
		cityComboBox.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				districtComboBox.removeAllItems();
				City city = ( City ) cityComboBox.getSelectedItem();
				if( city != null )
				{
					try
					{
						Vector< District > districts = new Vector< District >();
						districts = facade.findDistrict( new District( "", city.getId() ) ).toVector();
						districts.add( 0, null );
						
						for( District district : districts )
						{
							districtComboBox.addItem( district );
						}
					}
					catch ( RepositoryException e ) {}
				}
			}
			
		});
	}

	private void createButtons( String actionButtonFunction )
	{
		this.actionButton = ComponentFactory.createJButton( actionButtonFunction, true, 110, 22, 365, 250, null, getContentPane() );
		this.cancelButton = ComponentFactory.createJButton( "cancelar", true, 110, 22, 365, 280, null, getContentPane() );
		
		if( actionButtonFunction.equals( "inserir" ) ) actionButton.setMnemonic( KeyEvent.VK_I );
		else actionButton.setMnemonic( KeyEvent.VK_B );
		cancelButton.setMnemonic( KeyEvent.VK_C );
	}
	
	private void createCheckBox()
	{
		this.comungantCheckBox = ComponentFactory.createJCheckBox( 95, 245, getContentPane() );
	}

	private void createRadioButtons()
	{
		this.genderGroup = new ButtonGroup();
		this.maleRadioButton = ComponentFactory.createJRadioButton( "masculino", 95, 35, genderGroup, getContentPane() );
		this.femaleRadioButton = ComponentFactory.createJRadioButton( "feminino", 220, 35, genderGroup, getContentPane() );
	}

	public void setVisible( boolean arg0 )
	{
		super.setVisible( arg0 );
		if ( !arg0 )
		{
			source.setEnabled( true );
			source.setVisible( true );
		}
	}
}
